home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-04-16 | 2.5 KB | 87 lines | [TEXT/CWIE] |
- // IANarrow.h
- // Copyright: © 1994 - 1998 by Apple Computer, Inc., all rights reserved.
- //
- // Safe downcasting macros.
- //
- // These are needed if you use virtual inheritance (the recommended way to do
- // multiple inheritance in C++) and your C++ compiler does not yet support
- // RTTI (RunTime Type Information, aka dynamic_cast).
- //
- // Here's an example:
- // Suppose you have a base class called "Base". You have two
- // subclasses of it and then a third subclass that inherits from both
- // (the diamond pattern). Here are the declarations that need to be
- // done:
- // class Base {
- // IADefineNarrowMethods(Base, Base);
- // };
- // class Foo : public virtual Base {
- // IADefineNarrowMethods(Foo, Base);
- // };
- // class Bar : public virtual Base {
- // IADefineNarrowMethods(Bar, Base);
- // };
- // class Baz : public virtual Foo, public virtual Bar {
- // IADefineNarrowMethods(Baz, Base);
- // };
- //
- // Then in the implementation of Base, Foo, Bar, and Baz, put in:
- // IAImplementNarrowMethods0(Base);
- // IAImplementNarrowMethods1(Foo,Base);
- // IAImplementNarrowMethods1(Bar,Base);
- // IAImplementNarrowMethods2(Baz,Foo,Bar);
- //
- // Now to use the "IANarrow" method to down cast:
- //
- // Base *r = someBasePointer;
- // Foo *foo = Foo::IANarrow(r);
- // Bar *bar = Bar::IANarrow(r);
- // Baz *baz = Baz::IANarrow(r);
- //
- // Note that IANarrow returns NULL if the downcast failed.
- //
-
- #pragma once
- #ifndef IANarrow_h
- #define IANarrow_h
-
- #define IADefineNarrowMethods(CLASS, BASE)\
- static int IAClassID(); \
- static CLASS *IANarrow(BASE *t) { return (CLASS *)t->IANarrowInternal((long)&CLASS::IAClassID); } \
- virtual void *IANarrowInternal(long oid)
-
-
- #define IAImplementNarrowMethods0(CLASS) \
- int CLASS::IAClassID() { return 0; } \
- void *CLASS::IANarrowInternal(long oid) { \
- void *rval = 0; \
- if (oid == (long)&CLASS::IAClassID) \
- rval = this; \
- return rval; \
- }
-
- #define IAImplementNarrowMethods1(CLASS,PARENT) \
- int CLASS::IAClassID() { return 0; } \
- void *CLASS::IANarrowInternal(long oid) { \
- void *rval = 0; \
- if (oid == (long)&CLASS::IAClassID) \
- rval = this; \
- if (!rval) \
- rval = PARENT::IANarrowInternal(oid); \
- return rval; \
- }
-
- #define IAImplementNarrowMethods2(CLASS,PARENT1,PARENT2) \
- int CLASS::IAClassID() { return 0; } \
- void *CLASS::IANarrowInternal(long oid) { \
- void *rval = 0; \
- if (oid == (long)&CLASS::IAClassID) \
- rval = this; \
- if (!rval) \
- rval = PARENT1::IANarrowInternal(oid); \
- if (!rval) \
- rval = PARENT2::IANarrowInternal(oid); \
- return rval; \
- }
- #endif
-